
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using Yozeev.BusinessLogic;
using Yozeev.BusinessLogic.Dtos;
using Yozeev.BusinessLogic.DomainService;
using Yozeev.BusinessLogic.Authorization;
using Yozeev.SystemConfig.BasicConfig;
using Yozeev.Common;
using Abp.Authorization.Users;

namespace Yozeev.BusinessLogic
{
    /// <summary>
    /// TicketStation应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class TicketStationAppService : YozeevAppServiceBase, ITicketStationAppService
    {
        private readonly IRepository<TicketStation, Guid> _entityRepository;

		private readonly IRepository<Branch, Guid> _branchRepository;

		private readonly IQueryHelper _queryHelper;

		private readonly ITicketStationManager _entityManager;

        /// <summary>
        /// 构造函数 
        ///</summary>
        public TicketStationAppService(
        IRepository<TicketStation, Guid> entityRepository
        ,ITicketStationManager entityManager
			, IRepository<Branch, Guid> branchRepository
			,IQueryHelper queryHelper
		)
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
			_branchRepository = branchRepository;
			_queryHelper = queryHelper;
        }


        /// <summary>
        /// 获取TicketStation的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<TicketStationListDto>> GetPaged(GetTicketStationsInput input)
		{
			var expression = _queryHelper.WhereExp<TicketStation>(input.queryData);
			if (AbpUserBase.AdminUserName != GetCurrentUser().UserName)
				expression = _queryHelper.SetWhereExp<TicketStation>(expression, GetViewTypeEnum<TicketStation>(), await GetBranchUser());
			var query = from ticketStation in _entityRepository.GetAll().AsNoTracking()
						.Where(expression)
						select new
						{
							ticketStation,
							ticketStation.Branch,
                            ticketStation.ScenicSpot
						};
			var list = new List<TicketStation>();
			query.ToList().ForEach(temp => {
				var _ticketStation = temp.ticketStation;
				_ticketStation.Branch = temp.Branch;
                _ticketStation.ScenicSpot = temp.ScenicSpot;
				list.Add(_ticketStation);
			});
			// TODO:根据传入的参数添加过滤条件
			var count = list.Count();
			list = list.AsQueryable().OrderBy(input.Sorting)
					.PageBy(input)
					.ToList();
			var entityList = list;
			var entityListDtos =entityList.MapTo<List<TicketStationListDto>>();
			return new PagedResultDto<TicketStationListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取TicketStationListDto信息
		/// </summary>
		public async Task<TicketStationListDto> GetById(EntityDto<Guid> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<TicketStationListDto>();
		}

		/// <summary>
		/// 获取编辑 TicketStation
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<GetTicketStationForEditOutput> GetForEdit(NullableIdDto<Guid> input)
		{
			var output = new GetTicketStationForEditOutput();
TicketStationEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<TicketStationEditDto>();

				//ticketStationEditDto = ObjectMapper.Map<List<ticketStationEditDto>>(entity);
			}
			else
			{
				editDto = new TicketStationEditDto();
			}

			output.TicketStation = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改TicketStation的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task CreateOrUpdate(CreateOrUpdateTicketStationInput input)
		{

			if (input.TicketStation.Id.HasValue)
			{
				await Update(input.TicketStation);
			}
			else
			{
				await Create(input.TicketStation);
			}
		}


		/// <summary>
		/// 新增TicketStation
		/// </summary>
		protected virtual async Task<TicketStationEditDto> Create(TicketStationEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <TicketStation>(input);
            var entity=input.MapTo<TicketStation>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<TicketStationEditDto>();
		}

		/// <summary>
		/// 编辑TicketStation
		/// </summary>
		protected virtual async Task Update(TicketStationEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除TicketStation信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task Delete(EntityDto<Guid> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除TicketStation的方法
		/// </summary>
		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		/// <summary>
		/// 导出TicketStation为excel表,等待开发。
		/// </summary>
		/// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

    }
}


